﻿window.roomSelect = function (opts) {
    function disabledLeft() {
        $(navigation.left).removeClass("leftenabled");
        $(navigation.left).removeAttr("href");
    }
    function disabledRight() {
        $(navigation.right).removeClass("rightenabled");
        $(navigation.right).removeAttr("href");
    }
    function enabledLeft() {
        $(navigation.left).addClass("leftenabled");
        $(navigation.left).attr("href", "javascript:;");
    }
    function enabledRight() {
        $(navigation.right).addClass("rightenabled");
        $(navigation.right).attr("href", "javascript:;");
    }
    var roomSelector = $('#room_selectors_gallery');
    var morePhotoLink = $('#room_selectors_gallery div.more_photos a.head');
    var morePhotoBlock = $('#room_selectors_gallery div.more_photos');
    var navigation = {
        'left': '.left',
        'right': '.right'
    };
    var topPhotosGeneralBlock = $('#room_selectors_gallery ul.gallery_list');
    var topPhotos = $('#room_selectors_gallery').find(".gallery_list").find("li");

    var bottomPhotos = $('#room_selectors_gallery div.photos_slider ul li');
    var currentPhoto = 0;
    var isAnimating = false;
    var lastAnimate = new Date().getTime();
    var isPaused = true;
    if (roomSelector.length > 0) {

        if (currentPhoto == 0) {
            disabledLeft();
            //$(navigation.right).removeAttr("disabled");
        }
        if (topPhotos.length == 0) {
            $(navigation.left).hide();
            $(navigation.right).hide();
        } else if (topPhotos.length == 1) {
            disabledLeft();
            disabledRight();
        }
        else {
            $(navigation.left).show();
            $(navigation.right).show();
        }

        function alignBottomPhotos() {
            $('#room_selectors_gallery div.photos_slider ul').css('float', 'left');
            var bottomWidth = topPhotos.length * (60 + 20);
            if (bottomWidth != 0) {
                $('#room_selectors_gallery div.photos_slider ul').css({ 'left': ((1000 - bottomWidth) / 2) + "px" });
            }
            else {
                $('#room_selectors_gallery div.photos_slider ul').css({ 'left': ((1000 - 60) / 2) + "px" });
            }
        }
        alignBottomPhotos();
        var widthPhoto = $(topPhotos[0]).width();
        if (widthPhoto == 0) {
            widthPhoto = 1000;
        }

        function update(nextPhoto, force) {


            if (((new Date().getTime() - lastAnimate > 7000) || force) && !isAnimating) {

                if (nextPhoto > topPhotos.length - 1) {

                    nextPhoto = topPhotos.length - 1;
                    topPhotos = $('#room_selectors_gallery ul.gallery_list li');
                    //                    var tempItem = $(topPhotos[0]).clone();
                    //                    $(topPhotos[0]).remove();
                    //                    $('#room_selectors_gallery ul.gallery_list').append(tempItem);
                    $(topPhotosGeneralBlock).css('left', parseInt($(topPhotosGeneralBlock).css('left')) + widthPhoto);
                }
                if (nextPhoto < 0) {

                    nextPhoto = 0;
                    topPhotos = $('#room_selectors_gallery ul.gallery_list li');
                    //                    var tempItem = $(topPhotos[topPhotos.length - 1]).clone();
                    //                    $(topPhotos[topPhotos.length - 1]).remove();
                    //                    $('#room_selectors_gallery ul.gallery_list').prepend(tempItem);
                    $(topPhotosGeneralBlock).css('left', parseInt($(topPhotosGeneralBlock).css('left')) - widthPhoto);
                }

                var nextLeft = -nextPhoto * widthPhoto;
                isAnimating = true;
                $(topPhotosGeneralBlock).animate({ 'left': nextLeft }, 500, function () { isAnimating = false; });
                currentPhoto = nextPhoto;
                lastAnimate = new Date().getTime();
                if (nextPhoto == 0 && topPhotos.length > 1) {
                    disabledLeft();
                    enabledRight();
                }
                if (nextPhoto == topPhotos.length - 1 && topPhotos.length > 1) {
                    disabledRight();
                    enabledLeft();
                }
            }
        }
        function toGo(direction, force) {
            if (typeof force == "undefined") {
                force = false;
            }

            var nextPhoto = currentPhoto + direction;

            if (nextPhoto == -1) {
            	if (opts && opts.prePage){
            		window.location.href = opts.prePage;
            	}
                return;
            }
            if (nextPhoto == topPhotos.length) {
            	if (opts && opts.nextPage){
            		window.location.href = opts.nextPage;
            	}
                return;
            }


            update(nextPhoto, force);
        }
     
        //  window.setInterval(function () { if (!isPaused) { toGo(1, false); } }, 500);
        //        $(navigation.left).click(function (event) {

        //            toGo(-1, true);
        //            return false;
        //        });
        $(navigation.left).live("click", function () {

            toGo(-1, true);
            return false;
        });
        $(navigation.right).click(function (event) {

            toGo(1, true);
            return false;
        });
        $.each(bottomPhotos, function (index, item) {
            $(item).click(function (event) {

                update(index, true);
                return false;
            });
        });
        $(morePhotoBlock).mouseenter(function (event) {
            $(morePhotoBlock).animate({ 'bottom': '0px' }, 10);
        });
        $(morePhotoBlock).mouseleave(function (event) {
            $(morePhotoBlock).animate({ 'bottom': '-70px' }, 10);
        });


        //        $(roomSelector).mouseenter(function (event) {
        //            isPaused = true;
        //        });
        //        $(roomSelector).mouseleave(function (event) {
        //            isPaused = false;
        //        });
    }
}
 